package com.shujia.mr.hw.home3;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class Home3Part2Reducer extends Reducer<Text, Student, NullWritable,Student> {
    @Override
    protected void reduce(Text key, Iterable<Student> values, Reducer<Text, Student, NullWritable, Student>.Context context) throws IOException, InterruptedException {

        List<Student> students = new ArrayList<>();

        // 通过迭代器取出的Student对应的内存地址是同一个，
        //      所以在添加对象到ArrayList之前需要重新创建新的对象，再添加到集合中
        for (Student value : values) {
            students.add(new Student(value.getStudentID(),value.getName(),value.getAge(),value.getGender(),value.getClazz(),value.getTotalScore()));
        }

        Collections.sort(students);
        System.out.println(students);

        context.write(NullWritable.get(), students.get(0));
        context.write(NullWritable.get(), students.get(1));
        context.write(NullWritable.get(), students.get(2));

    }
}
